package com.net.shard.manager.impl;

import com.net.common.dao.BasicMapper;
import com.net.common.manager.AbstractManager;
import com.net.shard.dao.ShardSettingMapper;
import com.net.shard.domain.ShardSetting;
import com.net.shard.manager.ShardSettingManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**

 */
@Component
public class ShardSettingManagerImpl extends AbstractManager<ShardSetting> implements ShardSettingManager {
    private static final String SHARD_COUNT_KEY="shard_count";
    @Autowired
    private ShardSettingMapper mapper;

    @Override
    protected BasicMapper<Long, ShardSetting> getMapper() {
        return mapper;
    }

    @Override
    public int getShardCount(String dataSourceName) {
        ShardSetting setting = new ShardSetting();
        setting.setDataSourceName(dataSourceName);
        setting.setKey(SHARD_COUNT_KEY);
        ShardSetting shardSetting = mapper.selectOne(setting);
        if(shardSetting == null){
            throw new RuntimeException("Shard count not existed |" + dataSourceName);
        }
        try{
            return Integer.valueOf(shardSetting.getValue());
        }catch (Exception e){
            throw new RuntimeException("Shard count not illegal |" + dataSourceName +" | " + shardSetting.getId() +" |" + shardSetting.getValue(), e);
        }
    }
}
